package com.chenxi;

import java.util.concurrent.locks.ReentrantReadWriteLock;

// 写写互斥

public class MyWriteLock {

    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    public void write(){
        try{
            lock.writeLock().lock();        //写锁锁定
            System.out.println("获得写锁"+ Thread.currentThread().getName() + " " + System.currentTimeMillis());
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.writeLock().unlock();
        }
    }

    public static void main(String[] args) {
        MyWriteLock myWriteLock = new MyWriteLock();
        new Thread(new Runnable() {
            @Override
            public void run() {
                myWriteLock.write();
            }
        },"aaa").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                myWriteLock.write();
            }
        },"bbb").start();
    }
}
